library(tidyverse)
library(haven)
library(cowplot)

m2 <- read_dta("lab_pred_inspector.dta")
m5 <- read_dta("lab_pred_inspectee.dta")

m2$Role <- "Inspector (Politician)"
m5$Role <- "Inspectee (Bureaucrat)"

m2$b <- m2[["_margin"]]
m2$llci <- m2[["_ci_lb"]]
m2$ulci <- m2[["_ci_ub"]]
m2$c_factor <- m2[["_m1"]]+1
m2$f_factor <- m2[["_m2"]]+1

m5$b <- m5[["_margin"]]
m5$llci <- m5[["_ci_lb"]]
m5$ulci <- m5[["_ci_ub"]]
m5$c_factor <- m5[["_m1"]]+1
m5$f_factor <- m5[["_m2"]]+1

m2 <- select(m2, b, llci, ulci, c_factor, f_factor, Role)
m5 <- select(m5, b, llci, ulci, c_factor, f_factor, Role)

m_lab <- rbind(m2, m5)

m_lab$Rolefactor <- factor(m_lab$Role, 
                           levels = c("Inspector (Politician)", 
                                      "Inspectee (Bureaucrat)"))

m_lab$c <- ifelse(m_lab$c_factor == 1, "Low control costs", "High control costs")
m_lab$f <- ifelse(m_lab$f_factor == 1, "Low punishment fee", "High punishment fee")

m_lab$x <- paste0(m_lab$c, "\n", m_lab$f)

m_lab$xfactor <- factor(m_lab$x, levels = unique(m_lab$x)[c(1,3,2,4)])

m_lab$Rational <- c(0.66, 0.66, 0.33, 0.33, 0.8, 0.8, 0.2, 0.2)

saveRDS(m_lab, "precitions_lab.rds")

###########################################
## PSM and RISK PLOT
###########################################

psm_inspector <- read_dta("lab_psm_inspector.dta"); psm_inspector$Role <- "Inspector"; psm_inspector$x <- psm_inspector$psm_s01; psm_inspector$Var <- "PSM"
psm_inspectee <- read_dta("lab_psm_inspectee.dta"); psm_inspectee$Role <- "Inspectee";  psm_inspectee$x <- psm_inspectee$psm_s01; psm_inspectee$Var <- "PSM"
risk_inspector <- read_dta("lab_risk_inspector.dta"); risk_inspector$Role <- "Inspector"; risk_inspector$x <- risk_inspector$risk_beruf_r; risk_inspector$Var <- "Risk Aversion"
risk_inspectee <- read_dta("lab_risk_inspectee.dta"); risk_inspectee$Role <- "Inspectee"; risk_inspectee$x <- risk_inspectee$risk_beruf_r; risk_inspectee$Var <- "Risk Aversion"

df <- dplyr::bind_rows(list(psm_inspector, psm_inspectee, risk_inspector, risk_inspectee))

df$x <- df[["_at"]]/10
df$b <- df[["_margin"]]
df$llci <- df[["_ci_lb"]]
df$ulci <- df[["_ci_ub"]]

df$x <- as.numeric(df$x)

df$Role <- ifelse(df$Role == "Inspector", 
                  "Inspector\n(control)",
                  "Inspectee\n(high quality)") %>%
  factor(levels = c("Inspector\n(control)",
                    "Inspectee\n(high quality)"))

lab_psm <- df %>%
  filter(Var == "PSM") %>%
  ggplot(group = 1) + 
  geom_line(aes(x = x, y = b)) +
  geom_ribbon(aes(x = x, ymin = llci, ymax = ulci), 
              fill = "steelblue", alpha = 0.5, color = 0) +
  facet_grid( ~ Role, scales = "free_y") +
  theme_bw(base_size = 22) +
  xlab("PSM") +
  ylab("Prediction")

lab_risk <- df %>%
  filter(Var == "Risk Aversion") %>%
  ggplot(group = 1) + 
  geom_line(aes(x = x, y = b)) +
  geom_ribbon(aes(x = x, ymin = llci, ymax = ulci), 
              fill = "steelblue", alpha = 0.5, color = 0) +
  facet_grid( ~ Role, scales = "free_y") +
  theme_bw(base_size = 22) +
  xlab("Risk Aversion") +
  ylab("Prediction")

lab_psm_risk <- cowplot::plot_grid(lab_psm,
                                   lab_risk,
                                   ncol = 1)

ggsave(lab_psm_risk, file = "lab_predictions_psm_risk.jpeg", 
       width = 10, height = 8, dpi = 666)
ggsave(lab_psm_risk, file = "lab_predictions_psm_risk.pdf", width = 10, height = 8)
